<?php defined('RUN_KATANA') or die('Restricted access'); ?>
<?php 
if($shop->GlobalSettings['attributes'])
{
	$buttons_l[0]['image'] = "calculate_16.gif";
	$buttons_l[0]['onclick'] = "edit_form.submit();";
	$buttons_l[0]['onmousedown'] = "document.getElementById('recalculate_prices').value=1;";
	$buttons_l[0]['title'] = ucfirst($txt->getWord('recalculate').' '.$txt->getWord('prices'));
}

/*
$buttons_l[1]['image'] = "rebuild_22.gif";
$buttons_l[1]['onclick'] = "edit_form.submit();";
$buttons_l[1]['onmousedown'] = "document.getElementById('rebuild_identity').value=1;";
$buttons_l[1]['title'] = ucfirst($txt->getWord('rebuild').' '.$txt->getWord('identity'));
*/
if($shop->GlobalSettings['attributes'])
{
	$buttons_r[0]['image'] = "build_product_16.gif";
	$buttons_r[0]['onclick'] = "edit_form.submit();";
	$buttons_r[0]['onmousedown'] = "document.getElementById('build_variations_all').value=1;";
	$buttons_r[0]['title'] = ucfirst($txt->getWord('build').' '.$txt->getWord('variations'));
}
if($shop->getGlobalSetting('variations'))
{
	$buttons_r[1]['image'] = "delete_16.gif";
	$buttons_r[1]['onclick'] = "deleteDialog(-1);";
	$buttons_r[1]['title'] = ucfirst($txt->getWord('delete').' '.$txt->getWord('selected_elements'));
}
$buttons_l[1]['image'] = "visible_1_16.gif";
$buttons_l[1]['href'] = $commander->getFormAction().'&amp;item='.$shop->CurrentProduct->getId().'&display=all';
$buttons_l[1]['title'] = ucfirst($txt->getWord('view').' '.$txt->getWord('all'));

echo $commander->getListCommandsPanel('sa', '', $buttons_l, $buttons_r);
?>
<div class="menu_list">
	<input type="hidden" name="delete_var" id="del_var_id" value="0" />
    <input type="hidden" name="recalculate_prices" id="recalculate_prices" value="0" />
    <input type="hidden" name="rebuild_identity" id="rebuild_identity" value="0" />
    <input type="hidden" name="build_variations_all" id="build_variations_all" value="0" />
    <input type="hidden" name="build_variatons_on_att" id="build_variatons_on_att" value="0" />
	<table style="clear:both;" id="menulist" class="item_list" cellspacing="0" cellpadding="0" border="0">
		<thead class="fields">
	        <tr>
	            <th class="first center">&nbsp;&nbsp;Id&nbsp;&nbsp;</th>
	            <th><?php echo ucfirst($txt->getWord('choose')); ?></th>
	            <th>&nbsp;&nbsp;#&nbsp;&nbsp;</th>
	            <th><?php echo ucfirst($txt->getWord('description')); ?></th>
	            <th><?php echo ucfirst($txt->getWord('identity')); ?></th>
                <th><?php echo ucfirst($txt->getWord('ext_identity')); ?></th>
                <?php if($shop->GlobalSettings['buy_min']): ?>
                <th><?php echo ucfirst($txt->getWord('buy_min')); ?></th>
                <?php endif; ?>
                <th>
					<?php echo ucfirst($txt->getWord('std_price')); ?>
                    <?php if ($shop->getGlobalSetting('use_tax')) { echo ' ('.$txt->getWord('excl').'. '.$txt->getWord('tax').')'; } ?>
                </th>
                <?php if ($shop->getGlobalSetting('use_tax')): ?>
                <th><?php echo ucfirst($txt->getWord('std_price').' ('.$txt->getWord('inc').'. '.$txt->getWord('tax').')'); ?></th>
                <?php endif; ?>
                <?php if($shop->GlobalSettings['now_price']): ?>
                <th><?php echo ucfirst($txt->getWord('now_price')); ?></th>
                <th><?php echo ucfirst($txt->getWord('now_price')).' '.ucfirst($txt->getWord('ends')); ?></th>
                <?php endif; ?>
                <?php if($shop->GlobalSettings['offer_price']): ?>
                <th><?php echo ucfirst($txt->getWord('offer')).' '.ucfirst($txt->getWord('price')); ?></th>
                <th><?php echo ucfirst($txt->getWord('offer')).' '.ucfirst($txt->getWord('start')); ?></th>
                <th><?php echo ucfirst($txt->getWord('offer')).' '.ucfirst($txt->getWord('end')); ?></th>
                <?php endif; ?>
                <?php if($shop->GlobalSettings['stock']): ?>
                <th><?php echo ucfirst($txt->getWord('stock')); ?></th>
                <th><?php echo ucfirst($txt->getWord('stock')).' '.ucfirst($txt->getWord('date')); ?></th>
                <th>Add #</th>
                <th><?php echo ucfirst($txt->getWord('stock')); ?> Text</th>
                <?php endif; ?>
                <?php if($shop->GlobalSettings['shipping_weight_volume_active']): ?>
                <th>N.<?php echo ucfirst($txt->getWord('weight')); ?> (<?php echo $shop->CurrentProduct->WeightUnit; ?>)</th>
                <th>T.<?php echo ucfirst($txt->getWord('weight')); ?> (<?php echo $shop->CurrentProduct->WeightUnit; ?>)</th>
                <th><?php echo ucfirst($txt->getWord('length')); ?> (<?php echo $shop->CurrentProduct->DimUnit; ?>)</th>
                <th><?php echo ucfirst($txt->getWord('width')); ?> (<?php echo $shop->CurrentProduct->DimUnit; ?>)</th>
                <th><?php echo ucfirst($txt->getWord('height')); ?> (<?php echo $shop->CurrentProduct->DimUnit; ?>)</th>
                <?php endif; ?>
                <?php 
					if($shop->CurrentProduct->getAttributesVar())
					{
						$i=1;
						foreach($shop->CurrentProduct->getAttributesVar() as $attribute)
						{ echo '<th '.$class.'>'.$attribute['title'].'</th>'; }
					}
                ?>
                <th><?php echo ucfirst($txt->getWord('active')); ?></th>
	            <th class="last"><?php echo ucfirst($txt->getWord('delete')); ?></th>
	        </tr>
	    </thead>
	    <tbody>
        	<?php if($shop->getGlobalSetting('variations')): ?>
	        <tr class="items">
	            <td class="first center"><img 	title="<?php echo ucfirst($txt->getWord('new')); ?>"
                                                class="icon command" 
                                                src="<?php echo SYSICONS_URL; ?>add_16.gif" 
                                                onclick="if(document.getElementById('new_var').value != '') { edit_form.submit(); }" /></td>
	            <td><input title="<?php echo ucfirst($txt->getWord('all')); ?>" class="select_all" type="checkbox" name="select_all" value="" /></td>
                <td>&nbsp;</td>
	            <td><input class="small" name="new_var" type="text" value="" maxlength="50" id="new_var" /></td>
                <td><input class="small" name="new_var_identity" type="text" value="" maxlength="40" id="new_var_identity" /></td>
	            <td colspan="2">&nbsp;</td>
                <?php if ($shop->getGlobalSetting('use_tax')): ?>
                <td>&nbsp;</td>
                <?php endif; ?>
                <?php if($shop->GlobalSettings['now_price']): ?>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <?php endif; ?>
                <?php if($shop->GlobalSettings['offer_price']): ?>
                <td colspan="3">&nbsp;</td>
                <?php endif; ?>
				<?php if($shop->GlobalSettings['stock']): ?>
				<td colspan="4">&nbsp;</td>
                <?php endif; ?>
                <?php if($shop->GlobalSettings['shipping_weight_volume_active']): ?>
                <td colspan="5">&nbsp;</td>
                <?php endif; ?>
                <?php 
					if($shop->CurrentProduct->getAttributesVar())
					{
						$i=1;
						foreach($shop->CurrentProduct->getAttributesVar() as $attribute)
						{
							echo '<td '.$class.'>';
							if($attribute['predefined'] == 1)
							{
								$a = $shop->getAttributes();
								?>
								<select name="new_var_attr_<?php echo $attribute['id']; ?>_value_id">
									<?php 
									if($a[$attribute['id']]['predefined_values'])
									{
										foreach($a[$attribute['id']]['predefined_values'] as $v)
										{
											if($v['active'] > 0)
											{
												?>
												<option <?php echo getIsSelectedHTML($v['id'], $_POST['new_var_attr_'.$attribute['id'].'_value_id']) ?> value="<?php echo $v['id']; ?>"><?php echo $v['value']; ?></option>
												<?php 
											}
										}
									}
									?>
								</select>&nbsp;
                                <img title="<?php echo ucfirst($txt->getWord('build').' '.$txt->getWord('variations')); ?>" class="icon command build_v" src="<?php echo SYSICONS_URL; ?>build_16.gif" onclick="buildVariationsOnAtt(<?php echo $attribute['id']; ?>);" />
								<?php 
							}
							else
							{
								echo 	'<input type="text" name="attr_'.$attribute['id'].'_value[]" size="11" maxlength="98" value="" />';
							}
							echo '</td>'; 
						}
					}
                    ?>
                <td>&nbsp;</td>
	            <td class="last"></td>
	        </tr>
            <?php endif; ?>
            <?php 
			//echo 'display'.$_GET['display'];
			if(isset($shop->CurrentProduct->Variations))
			{
				$j=0;
				if($_GET['sort'] == 'desc')
				{ $variations = array_reverse($shop->CurrentProduct->Variations); }
				else
				{ $variations = $shop->CurrentProduct->Variations; }
				foreach($variations as $variation)
				{
					$j++;
					if(($j < 50) || ($_GET['display'] == 'all') || ($j < intval($_GET['display'])))
					{
				?>
                <tr class="items">
                	<td class="first center">
                    	<?php echo $variation->getId(); ?>
                        <input type="hidden" name="var_id[]" value="<?php echo $variation->getId(); ?>" />
                        <input type="hidden" name="var_varnum[]" value="<?php echo $variation->getVarnum(); ?>" />
                    </td>
                    <td>
                    	<?php if($shop->CurrentProduct->getFirstVariationId() != $variation->getId()): ?>
                    	<input class="select_list" type="checkbox" name="selected[]" value="<?php echo $variation->getId(); ?>" />
                        <?php endif; ?>
                    </td>
                    <td><?php echo $variation->getVarnum(); ?>.</td>
                    <td>
                    	<input class="max_200 small var_title_<?php echo $variation->getId(); ?> var_val" type="text" name="var_title[]" value="<?php echo $variation->getVarTitle(); ?>" id="var_title_<?php echo $variation->getId(); ?>" />
                    </td>
                    
                    <td>
                    	<input class="max_40 small identity_<?php echo $variation->getId(); ?> var_val" type="text" name="identity[]" value="<?php echo $variation->getIdentity(); ?>" id="identity_<?php echo $variation->getId(); ?>" maxlength="40" />
                    </td>
                    <td>
                    	<input class="max_40 xsmall ext_identity_<?php echo $variation->getId(); ?> var_val" type="text" name="ext_identity[]" value="<?php echo $variation->getExtIdentity(); ?>" id="ext_identity_<?php echo $variation->getId(); ?>" maxlength="40" />
                    </td>
                    <?php if($shop->GlobalSettings['buy_min']): ?>
                    <td>
                    	<input class="max_4 xsmall numeric int buy_min_<?php echo $variation->getId(); ?> var_val" type="text" name="buy_min[]" value="<?php echo $variation->getBuyMin(); ?>" id="buy_min_<?php echo $variation->getId(); ?>" maxlength="4" />
                    </td>
                    <?php endif; ?>
                    <td>
                    	<input class="max_14 xsmall numeric price std_price_<?php echo $variation->getId(); ?> var_val" type="text" name="std_price[]" value="<?php echo $variation->getStdPrice(); ?>" id="std_price_<?php echo $variation->getId(); ?>" maxlength="14" />
                    </td>
                    <?php if ($shop->getGlobalSetting('use_tax')): ?>
                    <td>
                    	<input class="max_14 xsmall numeric price std_price_tax_<?php echo $variation->getId(); ?> tax var_val" type="text" name="std_price_tax[]" value="<?php echo $variation->getStdPrice(true); ?>" id="std_price_tax_<?php echo $variation->getId(); ?>" maxlength="14" />
                    </td>
                    <?php endif; ?>
                    <?php if($shop->GlobalSettings['now_price']): ?>
                    <td>
                    	<input class="max_14 xsmall numeric price now_price_<?php echo $variation->getId(); ?> var_val" type="text" name="now_price[]" value="<?php echo $variation->getNowPrice(); ?>" id="now_price_<?php echo $variation->getId(); ?>" maxlength="14" />
                    </td>
                    <td>
                    	<input class="max_20 small-m NowPriceDateEnd var_val" id="npde<?php echo $variation->getId(); ?>" type="text" name="NowPriceDateEnd[]" value="<?php echo $variation->getNowPriceDateEnd(); ?>" maxlength="20" />
                    </td>
                    <?php endif; ?>
                    <?php if($shop->GlobalSettings['offer_price']): ?>
                    <td>
                    	<input class="max_14 xsmall numeric price offer_price_<?php echo $variation->getId(); ?> var_val" type="text" name="offer_price[]" value="<?php echo $variation->getOfferPrice(); ?>" id="offer_price_<?php echo $variation->getId(); ?>" maxlength="14" />
                    </td>
                    <td>
                    	<input alt="oend<?php echo $variation->getId(); ?>" id="ostart<?php echo $variation->getId(); ?>" class="max_20 small date offer-drs offer_start_<?php echo $variation->getId(); ?> var_val" type="text" name="offer_start[]" value="<?php echo $variation->getOfferStart(); ?>" maxlength="20" />
                    </td>
                    <td>
                    	<input alt="ostart<?php echo $variation->getId(); ?>" id="oend<?php echo $variation->getId(); ?>" class="max_20 small date offer-dre offer_end_<?php echo $variation->getId(); ?> var_val" type="text" name="offer_end[]" value="<?php echo $variation->getOfferEnd(); ?>" id="offer_end_<?php echo $variation->getId(); ?>" maxlength="20" />
                    </td>
                    <?php endif; ?>
                    <?php if($shop->GlobalSettings['stock']): ?>
                    <td>
                    	<input class="max_9 xxsmall numeric int stock_count_<?php echo $variation->getId(); ?> var_val" type="text" name="stock_count[]" value="<?php echo $variation->getStockCount(); ?>" id="stock_count_<?php echo $variation->getId(); ?>" maxlength="9" />
                    </td>
                    <td>
                    	<input class="max_20 small date stock_add_date_<?php echo $variation->getId(); ?> var_val" type="text" name="stock_add_date[]" value="<?php echo $variation->getStockAddDate(); ?>" id="stock_add_date_<?php echo $variation->getId(); ?>" maxlength="20" />
                    </td>
                    <td>
                    	<input class="max_5 xxsmall numeric int stock_add_quan_<?php echo $variation->getId(); ?> var_val" type="text" name="stock_add_quan[]" value="<?php echo $variation->getStockAddQuan(); ?>" id="stock_add_quan_<?php echo $variation->getId(); ?>" maxlength="5" />
                    </td>
                    <td>
                    	<input class="max_100 small stock_add_txt_<?php echo $variation->getId(); ?> var_val" type="text" name="stock_add_txt[]" value="<?php echo $variation->getStockAddTxt(); ?>" id="stock_add_txt_<?php echo $variation->getId(); ?>" />
                    </td>
                    <?php endif; ?>
                    <?php if($shop->GlobalSettings['shipping_weight_volume_active']): ?>
                    <td>
                    	<input class="max_9 xxsmall numeric int net_weight_<?php echo $variation->getId(); ?> var_val" type="text" name="net_weight[]" value="<?php echo $variation->getNetWeight(); ?>" id="net_weight_<?php echo $variation->getId(); ?>" maxlength="9" />
                    </td>
                    <td>
                    	<input class="max_9 xxsmall numeric int gros_weight_<?php echo $variation->getId(); ?> var_val" type="text" name="gros_weight[]" value="<?php echo $variation->getGrosWeight(); ?>" id="gros_weight_<?php echo $variation->getId(); ?>" maxlength="9" />
                    </td>
                    <td>
                    	<input class="max_9 xxsmall numeric int length_<?php echo $variation->getId(); ?> var_val" type="text" name="length[]" value="<?php echo $variation->getLength(); ?>" id="length_<?php echo $variation->getId(); ?>" maxlength="9" />
                    </td>
                    <td>
                    	<input class="max_9 xxsmall numeric int width_<?php echo $variation->getId(); ?> var_val" type="text" name="width[]" value="<?php echo $variation->getWidth(); ?>" id="width_<?php echo $variation->getId(); ?>" maxlength="9" />
                    </td>
					<td>
                    	<input class="max_9 xxsmall numeric int height_<?php echo $variation->getId(); ?> var_val" type="text" name="height[]" value="<?php echo $variation->getHeight(); ?>" id="height_<?php echo $variation->getId(); ?>" maxlength="9" />
                        <?php //print_r($shop->CurrentProduct->getAttributes()); ?>
                    </td>
                    <?php endif; ?>
                    <?php 
					if($shop->CurrentProduct->getAttributesVar())
					{
						$i=1;
						foreach($shop->CurrentProduct->getAttributesVar() as $attribute)
						{
							echo '<td '.$class.'>';
							if($attribute['predefined'] == 1)
							{
								$a = $shop->getAttributes();
								?>
								<select name="attr_<?php echo $attribute['id']; ?>_value_id_<?php echo $variation->getId(); ?>">
									<?php 
									if($a[$attribute['id']]['predefined_values'])
									{
										foreach($a[$attribute['id']]['predefined_values'] as $v)
										{
											if($v['visible'] > 0)
											{
												?>
												<option value="<?php echo $v['id']; ?>" <?php echo getIsSelectedHTML($v['id'], $variation->getAttributeValueId($attribute['id'])) ?>><?php echo $v['value']; ?></option>
												<?php 
											}
										}
									}
									?>
								</select>
                                <?php 
								
								?>
								<?php 
							}
							else
							{
								echo 	'<input type="text" name="attr_'.$attribute['id'].'_value[]" size="11" maxlength="98" value="" />';
							}
							echo '</td>'; 
						}
					}
                    ?>
                    <td>
                        <input type="checkbox" name="vActive[]" value="1" <?php if($variation->getActive() > 0) { echo 'checked'; } ?> />
                    </td>
                    <td class="last">&nbsp;
                    	<?php if($variation->getVarnum() > 1): ?>
                        <img title="<?php echo ucfirst($txt->getWord('delete').' '.$txt->getWord('product')); ?>" class="icon command delete_p" src="<?php echo SYSICONS_URL; ?>delete_16.gif" onclick="deleteDialog(<?php echo $variation->getId(); ?>);" />
                        <?php endif; ?>
                        <script type="text/javascript">
						$('input.std_price_tax_<?php echo $variation->getId(); ?>').change(function() 
						{
							var value = 0;
							value = Math.round(eval($(this).val().replace(/\,/gi, ".").replace(/[^\d\.]/gi, "") * <?php echo (1/$shop->CurrentProduct->getTaxValue()); ?>) * 100) / 100;
							$('input.std_price_<?php echo $variation->getId(); ?>').val(value);
						});
						$('input.std_price_<?php echo $variation->getId(); ?>').change(function() 
						{
							var value = 0;
							value = Math.round(eval($(this).val().replace(/\,/gi, ".").replace(/[^\d\.]/gi, "") * <?php echo ($shop->CurrentProduct->getTaxValue()); ?>) * 100) / 100;
							$('input.std_price_tax_<?php echo $variation->getId(); ?>').val(value);
						});
						</script>
                    </td>
            	</tr>
                <?php
					
				
					} 
				}
			}
			?>
	   </tbody>
	</table>
</div>
<script type="text/javascript">
function deleteDialog(itemId)
{
	jQuery('#delete_dialog').dialog('open');
	jQuery('#delete_dialog').dialog({
		bgiframe: true,
		resizable: false,
		height:140,
		modal: true,
		overlay: 
		{
			backgroundColor: '#000',
			opacity: 0.5
		},
		buttons: 
		{
			'<?php echo ucfirst($txt->getWord('no')); ?>': function() 
			{
				jQuery(this).dialog('close');
			},
			'<?php echo ucfirst($txt->getWord('delete').' '.$txt->getWord('variation')); ?>': function() 
			{
				jQuery('#del_var_id').val(itemId);
				jQuery(this).dialog('close');
				jQuery('#edit_form').submit();
			}
		}
	});
}

function buildVariationsOnAtt(attributeId)
{
	jQuery('#build_variatons_on_att').val(attributeId);
	jQuery('#edit_form').submit();
}

jQuery(".NowPriceDateEnd").datetimepicker(
{
	dateFormat: 'yy-mm-dd',
	timeFormat: "HH:mm:ss",
	changeMonth: true,
	numberOfMonths: 1,
	showWeek: true,
});

jQuery(".offer-drs").datetimepicker(
{
	dateFormat: "yy-mm-dd",
	timeFormat: "hh:mm:ss",
	changeMonth: true,
	numberOfMonths: 1,
	showWeek: true,
	onClose: function( selectedDate ) 
	{
		var target = jQuery(this).attr('alt');
		if(target != '')
		{ $("#"+target).datepicker( "option", "minDate", selectedDate ); }
	}
});
jQuery(".offer-dre").datetimepicker(
{
	dateFormat: "yy-mm-dd",
	timeFormat: "hh:mm:ss",
	changeMonth: true,
	numberOfMonths: 1, 
	showWeek: true, 
	showAnim: 'fadeIn',
	onClose: function( selectedDate ) 
	{
		var target = jQuery(this).attr('alt');
		if(target != '')
		{ $("#"+target).datepicker( "option", "maxDate", selectedDate ); }
	}
});
</script>
<div id="delete_dialog" title="<?php echo ucfirst($txt->getWord('delete').' '.$txt->getWord('variation')); ?>?" style="display:none; text-align:left;">
	<p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;"></span><?php echo ucfirst($txt->getWord('sure_you_want_to_delete')); ?></p>
</div>
<?php 
unset($shop->CurrentProduct);
?>